/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.danhMucDAO;
import dao.thanhPhoDAO;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Danhmuc;
import pojo.Thanhpho;

/**
 *
 * @author Administrator
 */
public class DanhMucController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("Action");
        String id = request.getParameter("id");

        if (action != null) {
            if (action.equals("View")) {
                List<Thanhpho> dstp = thanhPhoDAO.layDanhSachThanhPho();
                request.setAttribute("thanhpho", dstp);

                List<Danhmuc> ds = danhMucDAO.LayDanhMuc();
                request.setAttribute("DanhMuc", ds);


                request.getRequestDispatcher("/DanhMuc.jsp").forward(request, response);
                if (request.getParameter("btnEdit") != null) {

                    List<Danhmuc> d = danhMucDAO.layDanhMucTheoID(id);
                    request.setAttribute("danhmuctheoid", d);
                    request.getRequestDispatcher("/DanhMucEdit.jsp").forward(request, response);
                }
                if (request.getParameter("btnXoa") != null) {

                    List<Thanhpho> dstpa = thanhPhoDAO.layDanhSachThanhPho();
                    request.setAttribute("thanhpho", dstpa);

                    List<Danhmuc> dsb = danhMucDAO.LayDanhMuc();
                    request.setAttribute("DanhMuc", dsb);

                    request.getRequestDispatcher("/Home.jsp").forward(request, response);
                }
            } else if (action.equals("XuLy")) {
                List<Thanhpho> dstp = thanhPhoDAO.layDanhSachThanhPho();
                request.setAttribute("thanhpho", dstp);
                List<Danhmuc> ds = danhMucDAO.LayDanhMuc();
                request.setAttribute("DanhMuc", ds);
                List<Danhmuc> d = danhMucDAO.layDanhMucTheoID(id);
                request.setAttribute("danhmuctheoid", d);
                request.getRequestDispatcher("/DanhMucEdit.jsp").forward(request, response);
                if (request.getParameter("btnCapNhat") != null) {
                    String ma = request.getParameter("txtID");
                    String tendm = request.getParameter("txtTenDanhMuc");
                    String ngaytao = request.getParameter("txtNgayTao");
                    Danhmuc dm = new Danhmuc();//ds.get(0);
                    dm.setId(Integer.parseInt(ma));
                    Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
                    dm.setTenDanhMuc(tendm);

                    if ((ngaytao).isEmpty()) {
                        ngaytao = date.toString();
                        //dm.setNgayTao(date);
                    }
                    dm.setNgayTao(Timestamp.valueOf(ngaytao));
                    danhMucDAO.CapNhatDanhMuc(dm);

                    request.getRequestDispatcher("/Home.jsp").forward(request, response);

                }
                if (request.getParameter("btnXoa") != null) {
                    List<Thanhpho> dstp1 = thanhPhoDAO.layDanhSachThanhPho();
                    request.setAttribute("thanhpho", dstp1);
                    List<Danhmuc> ds1 = danhMucDAO.LayDanhMuc();
                    request.setAttribute("DanhMuc", ds1);
                    Danhmuc dm1 = ds1.get(0);
                    danhMucDAO.XoaDanhMuc(dm1);
                    request.getRequestDispatcher("/Home.jsp").forward(request, response);
                }
                if (request.getParameter("btnThem") != null) {
                    List<Thanhpho> dstp2 = thanhPhoDAO.layDanhSachThanhPho();
                    request.setAttribute("thanhpho", dstp2);
                    List<Danhmuc> ds2 = danhMucDAO.LayDanhMuc();
                    request.setAttribute("DanhMuc", ds2);
                    Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
                    Danhmuc dm2 = ds2.get(0);
                    String ten = request.getParameter("txtTenDanhMuc");
                    String ngay = request.getParameter("txtNgayTao");
                    dm2.setTenDanhMuc(ten);
                    dm2.setNgayTao(Timestamp.valueOf(ngay));
                    if ((ngay).isEmpty()) {
                        dm2.setNgayTao(date);
                    }
                    danhMucDAO.ThemDanhMuc(dm2);
                    request.getRequestDispatcher("/Home.jsp").forward(request, response);
                }
            } else if (action.equals("EditDanhMuc")) {
                List<Thanhpho> dstp = thanhPhoDAO.layDanhSachThanhPho();
                request.setAttribute("thanhpho", dstp);
                List<Danhmuc> ds = danhMucDAO.LayDanhMuc();
                request.setAttribute("DanhMuc", ds);
                List<Danhmuc> d = danhMucDAO.layDanhMucTheoID(id);
                request.setAttribute("danhmuctheoid", d);
                request.getRequestDispatcher("DanhMucEdit.jsp").forward(request, response);
                if (request.getParameter("btnCapNhat") != null) {
                    String ma = request.getParameter("txtID");
                    String tendm = request.getParameter("txtTenDanhMuc");
                    String ngaytao = request.getParameter("txtNgayTao");
                    Danhmuc dm = new Danhmuc();
                    dm.setId(Integer.parseInt(ma));
                    Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
                    dm.setTenDanhMuc(tendm);

                    if ((ngaytao).isEmpty()) {
                        ngaytao = date.toString();
                    }
                    dm.setNgayTao(Timestamp.valueOf(ngaytao));

                    List<Danhmuc> dsdm = danhMucDAO.layDanhMucTheoID(id);
                    request.setAttribute("danhmuc", dsdm);
                    danhMucDAO.CapNhatDanhMuc(dm);
                    request.getRequestDispatcher("/Home.jsp").forward(request, response);

                }

            } else if (action.equals("ThemDanhMuc")) {

                List<Thanhpho> dstp1 = thanhPhoDAO.layDanhSachThanhPho();
                request.setAttribute("thanhpho", dstp1);
                List<Danhmuc> ds1 = danhMucDAO.LayDanhMuc();
                request.setAttribute("DanhMuc", ds1);
                request.getRequestDispatcher("/ThemDanhMuc.jsp").forward(request, response);
                if (request.getParameter("btnThem") != null) {
                    Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
                    Danhmuc dm1 = new Danhmuc();
                    String ten = request.getParameter("txtTenDanhMuc");
                    String ngay = request.getParameter("txtNgayTao");
                    dm1.setTenDanhMuc(ten);

                    if ((ngay).isEmpty()) {
                        ngay = date.toString();
                    }
                    dm1.setNgayTao(Timestamp.valueOf(ngay));
                    boolean kq = danhMucDAO.ThemDanhMuc(dm1);
                    if (kq == true) {
                        request.getRequestDispatcher("/Home.jsp").forward(request, response);
                    }


                }


            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
